package cn.bocweb.spinning.model.Imp;

import android.util.Log;

import com.squareup.okhttp.Interceptor;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import cn.bocweb.spinning.model.Factory.ApiHeaders;
import cn.bocweb.spinning.net.UserServer;
import cn.bocweb.spinning.util.Constant;
import retrofit.GsonConverterFactory;
import retrofit.Retrofit;


/**
 * Created by chenchangxing on 16/1/7.
 */
public class BaseModelImp {
    private Retrofit retrofit;
    public UserServer mServer;

    public BaseModelImp() {
        OkHttpClient httpClient = new OkHttpClient();
        httpClient.interceptors().add(new Interceptor() {
            @Override
            public Response intercept(Chain chain) throws IOException {
                Request request = chain.request();
                Log.e("Request", request.urlString());
                Response response = chain.proceed(request);
                Log.e("TAG", response.request().urlString());
                Log.e("TAG", response.body().string());
                return response;
            }
        });
        if (retrofit == null) {

            OkHttpClient delay = new OkHttpClient();
            delay.setConnectTimeout(15 * 1000, TimeUnit.MILLISECONDS);
            delay.interceptors().add(new ApiHeaders());

            retrofit = new Retrofit.Builder()
                    .baseUrl(Constant.BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .client(delay)
                    .build();
        }
        if (mServer == null) {
            mServer = retrofit.create(UserServer.class);
        }
    }
}
